﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuanLyKho.Server;


namespace QuanLyKho.Server.BLL
{
    public class PhieuXuatThuocBLL
    {
        public PhieuXuatThuocBLL()
        {
        }

        public int CreatePhieuXuat(PhieuXuatThuoc phieuXuat)
        {
            int result;
            using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
            {
                entities.PhieuXuatThuocs.Add(phieuXuat);
                result = entities.SaveChanges();
            }
            return result;
        }

        public void UpdatePhieuXuat(PhieuXuatThuoc phieuXuat)
        {
            using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
            {
                PhieuXuatThuoc item = entities.PhieuXuatThuocs.Single(u => u.IdPhieuXuat == phieuXuat.IdPhieuXuat);
                item.NgayXuat = phieuXuat.NgayXuat;
                entities.SaveChanges();
            }
        }

        public List<PhieuXuatThuoc> RetrieveAll()
        {
            List<PhieuXuatThuoc> list = new List<PhieuXuatThuoc>();
            using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
            {
                list = entities.PhieuXuatThuocs.OrderByDescending(u => u.IdPhieuXuat).ToList();
            }
            return list;
        }

        public List<PhieuXuatThuoc> RetrieveTop10()
        {
            List<PhieuXuatThuoc> list = new List<PhieuXuatThuoc>();
            using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
            {
                list = entities.PhieuXuatThuocs.OrderByDescending(u => u.IdPhieuXuat).Take(10).ToList();
            }
            return list;
        }

        public List<PhieuXuatThuoc> RetrievePhieuXuatByDate(DateTime tuNgay, DateTime denNgay)
        {
            tuNgay = Convert.ToDateTime(tuNgay.ToShortDateString() + " 00:00:00");
            denNgay = Convert.ToDateTime(denNgay.ToShortDateString() + " 23:59:59");
            List<PhieuXuatThuoc> list = new List<PhieuXuatThuoc>();
            using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
            {
                list = entities.PhieuXuatThuocs.Where(u=>u.NgayXuat >= tuNgay && u.NgayXuat <= denNgay).OrderByDescending(u => u.IdPhieuXuat).ToList();
            }
            return list;
        }
    }
}
